<template>
  <div id="uploadProgress">
    <section class="header">
      <h1>上传进度</h1>
      <aside>
        <span><router-link to="/study/studySchedule">点击这里返回吧！</router-link></span>
      </aside>
    </section>
    <section class="main">
      <div>姓名： &nbsp;&nbsp;&nbsp;&nbsp; <span>{{ username }}</span></div>
      <div>选择日期：<span>{{ time }}</span></div>
      <div>内容填写：<textarea name="" id="" placeholder="点击这里编辑内容" v-model="content" maxlength="100"></textarea></div>
      <section>
        <a-upload
            :beforeUpload="beforeUpload"
            v-model:file-list="fileList"
            maxCount="1"
            name="file"
            action="http://119.23.46.241:11900/api/v1/user/study/schedule"
            :headers="uploadHeader"
            @change="handleChange"
            :remove="imgRemove"
            accept=".jpg,.jpeg,.png,.JPG,JPEG,.PNG"
        >
          <a-button>
            <upload-outlined></upload-outlined>
            图片
          </a-button>
        </a-upload>
        <a-button id="submit" @click="updateSchedule" type="primary" :loading="loading">上传</a-button>
      </section>
    </section>
  </div>
</template>
<script>
import {defineComponent, getCurrentInstance, ref} from "vue";
import {useRouter} from 'vue-router'
import {message} from 'ant-design-vue';
import {UploadOutlined} from '@ant-design/icons-vue';
import {PoweroffOutlined} from '@ant-design/icons-vue';
import $store from "@/store";
import axios from "axios";

export default defineComponent({
  name: "uploadProgress",
  setup() {
    const {proxy} = getCurrentInstance();
    const router = useRouter()
    let username = ref();
    let time = new Date().toLocaleDateString()
    let content = ref(),
        files = ref();
    let date = new Date(time).getTime() / 1000;
    let uploadHeader = ref();
    let fileList = ref([]);
    let loading = ref(false)

    //获取登录用户
    proxy.$request(proxy.$getRoute(8).route_method, proxy.$getRoute(8).route_url, {}).then(res => {
      username.value = res.data.username;
    })

    let token = $store.state.token
    uploadHeader.value = {
      Authorization: token
    }

    function updateSchedule() {
      loading.value = true
      if (content.value) {
        const formData = new FormData();
        formData.append('date', date);
        formData.append('content', content.value);
        formData.append('img', files.value);

        axios.defaults.headers.post['Content-Type'] = 'multipart/form-data';
        axios.post(proxy.$getRoute(100).route_url, formData).then(res => {
          loading.value = false
          if (res.data.code === 200) {
            message.success('学习进度上传成功')
            setTimeout(()=>{
              router.push({path: 'studySchedule'})
            },500)

          } else {
            message.error(res.data.msg)
          }
        })
      } else {
        message.error('请填写内容哦！')
      }

    }

    const beforeUpload = file => {
      files.value = file
      return false
    }

    const imgRemove = () => {
      files.value = null
    }

    const handleChange = info => {
      if (info.fileList.length === 2) {
        fileList.value = fileList.value.slice(-1)
        info.fileList = info.fileList.slice(-1);
        message.warning("只能上传一个文件")
      }

      if (info.file.status !== 'uploading') {
        // console.log(info.file, info.fileList);
      }

      if (info.file.status === 'done') {
        message.success(`${info.file.name} file uploaded successfully`);
      } else if (info.file.status === 'error') {
        message.error(`${info.file.name} file upload failed.`);
      }
    }


    return {
      username,
      time,
      files,
      content,
      updateSchedule,
      beforeUpload,
      imgRemove,
      loading,
      uploadHeader,
      fileList,
      handleChange,
    }
  },
  components: {
    UploadOutlined,
    PoweroffOutlined,
  },
})
</script>
<style lang="less">
body {
  background: #FFF;
}

ul {
  list-style: none;
}

#uploadProgress {
  width: 630px;
  height: 92vh;
  margin: 0 auto;
  padding-top: 65px;
  box-sizing: border-box;

  .header {
    width: 100%;
    height: 184px;
    background: #FFFFFF;
    border-radius: 0px 0px 68px 16px;
    display: flex;
    flex-direction: column;
    padding: 0 30px;
    box-sizing: border-box;

    h1 {
      font-size: 36px;
      font-family: PingFang SC-Medium, PingFang SC;
      font-weight: 500;
      color: #262626;
      line-height: 54px;
      margin: 40px auto 32px;
    }

    aside {
      width: 360px;
      display: flex;
      justify-content: center;
      align-items: center;
      margin: 0 auto;

      span {
        font-size: 16px;
        font-family: PingFang SC-Medium, PingFang SC;
        font-weight: 500;
        color: #6777EF;
        line-height: 19px;
        cursor: pointer;
      }
    }
  }

  .main {
    display: flex;
    justify-content: start;
    flex-direction: column;

    & > div {
      width: 100%;
      height: 25px;
      font-size: 18px;
      font-family: PingFang SC-Regular, PingFang SC;
      font-weight: 400;
      color: #262626;
      line-height: 21px;
      margin-bottom: 44px;

      span {
        color: #465059;
      }

      &:nth-child(3) {
        display: flex;
        justify-content: start;
        height: 182px;
        margin-bottom: 22px;

        textarea {
          width: 522px;
          height: 182px;
          border-radius: 8px 8px 8px 8px;
          border: 1px solid #EEEEEE;
          padding: 16px;
          font-size: 16px;
          color: #465059;
          box-sizing: border-box;
          outline: none;
          resize: none;
        }
      }
    }

    section {
      position: relative;
      //display: flex;
      //justify-content: space-between;
      width: 522px;
      height: 300px;
      margin-left: 90px;

      div .ant-btn {
        width: 77px;
        height: 40px;
        text-align: center;
        line-height: 40px;
        border: none;
        color: #FFF;
        border-radius: 6px 6px 6px 6px;
        padding: 0;
        opacity: 1;
        cursor: pointer;

        span {
          font-size: 14px;
          color: #FFF;
          margin: 0;
        }

        .anticon {
          display: none;
        }
      }

      .ant-btn {
        background: #262626;
      }

      #submit {
        width: 80px;
        height: 40px;
        text-align: center;
        line-height: 40px;
        border: none;
        color: #FFF;
        border-radius: 6px 6px 6px 6px;
        padding: 0;
        opacity: 1;
        cursor: pointer;

        position: absolute;
        right: 0;
        top: 0;
        background: #6777EF;
      }
    }
  }
}
</style>